home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / pibt3sp2.arc / PIBFHIO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-07-05  |  18.3 KB  |  372 lines

  1. (*----------------------------------------------------------------------*)
  2. (*        PIBFHIO.PAS --- File Handle Input/Output for Turbo Pascal     *)
  3. (*----------------------------------------------------------------------*)
  4. (*                                                                      *)
  5. (*  Author:  Philip R. Burns                                            *)
  6. (*  Version: 1.0  (June, 1985)                                          *)
  7. (*                                                                      *)
  8. (*  Systems: For MS-DOS on IBM PCs and close compatibles only.          *)
  9. (*           Note:  I have checked these on Zenith 151s under           *)
  10. (*                  MSDOS 2.1 and IBM PCs under PCDOS 2.0.              *)
  11. (*                                                                      *)
  12. (*  Needs:   Global types from GLOBTYPE.PAS.                            *)
  13. (*                                                                      *)
  14. (*  History: Original with me.                                          *)
  15. (*                                                                      *)
  16. (*           Suggestions for improvements or corrections are welcome.   *)
  17. (*           Please leave messages on Gene Plantz's BBS (312) 882 4145  *)
  18. (*           or Ron Fox's BBS (312) 940 6496.                           *)
  19. (*                                                                      *)
  20. (*           IF you use this code in your own programs, please be nice  *)
  21. (*           and give proper credit.                                    *)
  22. (*                                                                      *)
  23. (*----------------------------------------------------------------------*)
  24. (*                                                                      *)
  25. (*  Usage:   These routines provide a Turbo Pascal interface to the     *)
  26. (*           MS DOS file handle style input/output routines.  These are *)
  27. (*           used in PibTerm for I/O during uploading and downloading,  *)
  28. (*           as well as for copying files.  Use of these routines       *)
  29. (*           allows precise control of file sizes and avoids various    *)
  30. (*           bugs in the Turbo Block I/O routines.                      *)
  31. (*                                                                      *)
  32. (*----------------------------------------------------------------------*)
  33. (*                                                                      *)
  34. (*  Routines:                                                           *)
  35. (*                                                                      *)
  36. (*      Open_File_Handle                                                *)
  37. (*      Close_File_Handle                                               *)
  38. (*      Create_File_Handle                                              *)
  39. (*      Read_File_Handle                                                *)
  40. (*      Write_File_Handle                                               *)
  41. (*                                                                      *)
  42. (*----------------------------------------------------------------------*)
  43.  
  44. (*----------------------------------------------------------------------*)
  45. (*                  Constants for file handle access                    *)
  46. (*----------------------------------------------------------------------*)
  47.  
  48. CONST
  49.    Access_Read_Mode           = 0;
  50.    Access_Write_Mode          = 1;
  51.    Access_Read_And_Write_Mode = 2;
  52.  
  53.    Attribute_None             = 0;
  54.    Attribute_Read_Only        = 1;
  55.    Attribute_Hidden           = 2;
  56.    Attribute_System           = 4;
  57.    Attribute_Volume_Label     = 8;
  58.    Attribute_Subdirectory     = 16;
  59.    Attribute_Archive          = 32;
  60.  
  61. (*----------------------------------------------------------------------*)
  62. (*            Open_File_Handle --- Opens file using file handle         *)
  63. (*----------------------------------------------------------------------*)
  64.  
  65. FUNCTION Open_File_Handle(     File_Name:   AnyStr;
  66.                                File_Access: INTEGER;
  67.                            VAR File_Handle: INTEGER ) : INTEGER;
  68.  
  69. (*----------------------------------------------------------------------*)
  70. (*                                                                      *)
  71. (*     Function:   Open_File_Handle                                     *)
  72. (*                                                                      *)
  73. (*     Purpose:    Opens file using file handle                         *)
  74. (*                                                                      *)
  75. (*     Calling Sequence:                                                *)
  76. (*                                                                      *)
  77. (*        Error := Open_File_Handle(     File_Name:   AnyStr;           *)
  78. (*                                       File_Access: INTEGER;          *)
  79. (*                                   VAR File_Handle: INTEGER ) :       *)
  80. (*                                       INTEGER;                       *)
  81. (*                                                                      *)
  82. (*           File_Name   --- path name of file to be opened             *)
  83. (*           File_Access --- mode to access file                        *)
  84. (*                           0 = read only                              *)
  85. (*                           1 = write only                             *)
  86. (*                           2 = read and write                         *)
  87. (*           File_Handle --- returned file handle                       *)
  88. (*           Error       --- DOS error return code                      *)
  89. (*                                                                      *)
  90. (*     Calls:                                                           *)
  91. (*                                                                      *)
  92. (*        MsDos                                                         *)
  93. (*                                                                      *)
  94. (*----------------------------------------------------------------------*)
  95.  
  96. VAR
  97.    Reg  : RegPack;
  98.  
  99. BEGIN (* Open_File_Handle *)
  100.                                    (* Convert path name to Ascii Z string *)
  101.  
  102.    Convert_String_To_AsciiZ( File_Name );
  103.  
  104.                                    (* Set parameters for open file handle *)
  105.    Reg.Al := File_Access;
  106.    Reg.Ah := $3D;
  107.    Reg.Ds := SEG( File_Name[1] );
  108.    Reg.Dx := OFS( File_Name[1] );
  109.  
  110.                                    (* Open file, get handle *)
  111.    MsDos( Reg );
  112.                                    (* Check for bad return  *)
  113.  
  114.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  115.       BEGIN
  116.          Open_File_Handle := 0;
  117.          File_Handle      := Reg.Ax;
  118.       END
  119.    ELSE
  120.       BEGIN
  121.          Open_File_Handle := Reg.Ax;
  122.          File_Handle      := 0;
  123.       END;
  124.  
  125. END   (* Open_File_Handle *);
  126.  
  127. (*----------------------------------------------------------------------*)
  128. (*                Close_File_Handle --- Closes file handle              *)
  129. (*----------------------------------------------------------------------*)
  130.  
  131. FUNCTION Close_File_Handle( File_Handle: INTEGER ) : INTEGER;
  132.  
  133. (*----------------------------------------------------------------------*)
  134. (*                                                                      *)
  135. (*     Function:   Close_File_Handle                                    *)
  136. (*                                                                      *)
  137. (*     Purpose:    Closes file handle                                   *)
  138. (*                                                                      *)
  139. (*     Calling Sequence:                                                *)
  140. (*                                                                      *)
  141. (*        Error := Close_File_Handle( File_Handle: INTEGER ): INTEGER;  *)
  142. (*                                                                      *)
  143. (*           File_Handle --- File handle of file to close               *)
  144. (*           Error       --- DOS error return code                      *)
  145. (*                                                                      *)
  146. (*     Calls:                                                           *)
  147. (*                                                                      *)
  148. (*        MsDos                                                         *)
  149. (*                                                                      *)
  150. (*----------------------------------------------------------------------*)
  151.  
  152. VAR
  153.    Reg  : RegPack;
  154.  
  155. BEGIN (* Close_File_Handle *)
  156.  
  157.                                    (* Set parameters for close file handle *)
  158.    Reg.Ah := $3E;
  159.    Reg.Bx := File_Handle;
  160.                                    (* Close the file handle *)
  161.    MsDos( Reg );
  162.                                    (* Check for bad return  *)
  163.  
  164.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  165.       Close_File_Handle := 0
  166.    ELSE
  167.       Close_File_Handle := Reg.Ax;
  168.  
  169. END   (* Close_File_Handle *);
  170.  
  171. (*----------------------------------------------------------------------*)
  172. (*           Read_File_Handle --- Performs read using file handle       *)
  173. (*----------------------------------------------------------------------*)
  174.  
  175. FUNCTION Read_File_Handle(     File_Handle: INTEGER;
  176.                            VAR File_Buffer  ;
  177.                            VAR Read_Length: INTEGER ) : INTEGER;
  178.  
  179. (*----------------------------------------------------------------------*)
  180. (*                                                                      *)
  181. (*     Function:   Read_File_Handle                                     *)
  182. (*                                                                      *)
  183. (*     Purpose:    Reads file using file handle                         *)
  184. (*                                                                      *)
  185. (*     Calling Sequence:                                                *)
  186. (*                                                                      *)
  187. (*        Error := Read_File_Handle(     File_Handle: INTEGER;          *)
  188. (*                                   VAR File_Buffer  ;                 *)
  189. (*                                   VAR Read_Length: INTEGER ) :       *)
  190. (*                                           INTEGER;                   *)
  191. (*                                                                      *)
  192. (*           File_Handle --- File handle of file to read                *)
  193. (*           File_Buffer --- Buffer area to receive data read           *)
  194. (*           Read_Length --- On input, is number of characters to read. *)
  195. (*                           On output, is number of chars actually     *)
  196. (*                           read                                       *)
  197. (*           Error       --- DOS error return code                      *)
  198. (*                                                                      *)
  199. (*     Calls:                                                           *)
  200. (*                                                                      *)
  201. (*        MsDos                                                         *)
  202. (*                                                                      *)
  203. (*----------------------------------------------------------------------*)
  204.  
  205. VAR
  206.    Reg  : RegPack;
  207.  
  208. BEGIN (* Read_File_Handle *)
  209.                                    (* Set parameters for read *)
  210.    WITH Reg DO
  211.       BEGIN
  212.  
  213.          Ah := $3F;
  214.          Bx := File_Handle;
  215.          Cx := Read_Length;
  216.          Ds := SEG( File_Buffer );
  217.          Dx := OFS( File_Buffer );
  218.  
  219.       END;
  220.                                    (* Perform the read *)
  221.    MsDos( Reg );
  222.                                    (* Check for bad return  *)
  223.  
  224.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  225.       BEGIN
  226.          Read_File_Handle := 0;
  227.          Read_Length      := Reg.Ax;
  228.       END
  229.    ELSE
  230.       BEGIN
  231.          Read_File_Handle := Reg.Ax;
  232.          Read_Length      := 0;
  233.       END;
  234.  
  235. END   (* Read_File_Handle *);
  236.  
  237. (*----------------------------------------------------------------------*)
  238. (*          Write_File_Handle --- Performs read using file handle       *)
  239. (*----------------------------------------------------------------------*)
  240.  
  241. FUNCTION Write_File_Handle(     File_Handle: INTEGER;
  242.                             VAR File_Buffer  ;
  243.                             VAR Write_Length: INTEGER ) : INTEGER;
  244.  
  245. (*----------------------------------------------------------------------*)
  246. (*                                                                      *)
  247. (*     Function:   Write_File_Handle                                    *)
  248. (*                                                                      *)
  249. (*     Purpose:    Writes file using file handle                        *)
  250. (*                                                                      *)
  251. (*     Calling Sequence:                                                *)
  252. (*                                                                      *)
  253. (*        Error := Write_File_Handle(     File_Handle: INTEGER;         *)
  254. (*                                    VAR File_Buffer ;                 *)
  255. (*                                    VAR Write_Length: INTEGER ):      *)
  256. (*                                            INTEGER;                  *)
  257. (*                                                                      *)
  258. (*           File_Handle  --- File handle of file to write              *)
  259. (*           File_Buffer  --- Buffer area from which to write data      *)
  260. (*           Write_Length --- On input, is number of chars. to write.   *)
  261. (*                            On output, is number of chars actually    *)
  262. (*                            written.                                  *)
  263. (*           Error        --- DOS error return code                     *)
  264. (*                                                                      *)
  265. (*     Calls:                                                           *)
  266. (*                                                                      *)
  267. (*        MsDos                                                         *)
  268. (*                                                                      *)
  269. (*----------------------------------------------------------------------*)
  270.  
  271. VAR
  272.    Reg  : RegPack;
  273.  
  274. BEGIN (* Write_File_Handle *)
  275.                                    (* Set parameters for write *)
  276.    WITH Reg DO
  277.       BEGIN
  278.  
  279.          Ah := $40;
  280.          Bx := File_Handle;
  281.          Cx := Write_Length;
  282.          Ds := SEG( File_Buffer );
  283.          Dx := OFS( File_Buffer );
  284.  
  285.       END;
  286.                                    (* Perform the read *)
  287.    MsDos( Reg );
  288.                                    (* Check for bad return  *)
  289.  
  290.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  291.       BEGIN
  292.          Write_File_Handle := 0;
  293.          Write_Length      := Reg.Ax;
  294.       END
  295.    ELSE
  296.       BEGIN
  297.          Write_File_Handle := Reg.Ax;
  298.          Write_Length      := 0;
  299.       END;
  300.  
  301. END   (* Write_File_Handle *);
  302.  
  303. (*----------------------------------------------------------------------*)
  304. (*          Create_File_Handle --- Creates file and file handle         *)
  305. (*----------------------------------------------------------------------*)
  306.  
  307. FUNCTION Create_File_Handle(     File_Name:   AnyStr;
  308.                                  File_Attrib: INTEGER;
  309.                              VAR File_Handle: INTEGER ) : INTEGER;
  310.  
  311. (*----------------------------------------------------------------------*)
  312. (*                                                                      *)
  313. (*     Function:   Create_File_Handle                                   *)
  314. (*                                                                      *)
  315. (*     Purpose:    Creates file and file handle                         *)
  316. (*                                                                      *)
  317. (*     Calling Sequence:                                                *)
  318. (*                                                                      *)
  319. (*        Error := Create_File_Handle(     File_Name:   AnyStr;         *)
  320. (*                                         File_Attrib: INTEGER;        *)
  321. (*                                     VAR File_Handle: INTEGER ) :     *)
  322. (*                                             INTEGER;                 *)
  323. (*                                                                      *)
  324. (*           File_Name   --- path name of file to be created            *)
  325. (*           File_Attrib --- attributes for file:                       *)
  326. (*                           None          = 0;                         *)
  327. (*                           Read_Only     = 1;                         *)
  328. (*                           Hidden        = 2;                         *)
  329. (*                           System        = 4;                         *)
  330. (*                           Volume_Label  = 8;                         *)
  331. (*                           Subdirectory  = 16;                        *)
  332. (*                           Archive       = 32;                        *)
  333. (*           File_Handle --- returned file handle                       *)
  334. (*           Error       --- DOS error return code                      *)
  335. (*                                                                      *)
  336. (*     Calls:                                                           *)
  337. (*                                                                      *)
  338. (*        MsDos                                                         *)
  339. (*                                                                      *)
  340. (*----------------------------------------------------------------------*)
  341.  
  342. VAR
  343.    Reg  : RegPack;
  344.  
  345. BEGIN (* Create_File_Handle *)
  346.                                    (* Convert path name to Ascii Z string *)
  347.  
  348.    Convert_String_To_AsciiZ( File_Name );
  349.  
  350.                                    (* Set parameters for create file handle *)
  351.    Reg.Cx := File_Attrib;
  352.    Reg.Ah := $3C;
  353.    Reg.Ds := SEG( File_Name[1] );
  354.    Reg.Dx := OFS( File_Name[1] );
  355.  
  356.                                    (* Open file, get handle *)
  357.    MsDos( Reg );
  358.                                    (* Check for bad return  *)
  359.  
  360.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  361.       BEGIN
  362.          Create_File_Handle := 0;
  363.          File_Handle        := Reg.Ax;
  364.       END
  365.    ELSE
  366.       BEGIN
  367.          Create_File_Handle := Reg.Ax;
  368.          File_Handle        := 0;
  369.       END;
  370.  
  371. END   (* Create_File_Handle *);
  372.